
unit MainFrm;

interface

uses
  SysUtils, Windows, Classes, Graphics, Forms, Controls, 
  MPlayer, StdCtrls,Menus,  Messages, Buttons,   
  Dialogs, ExtCtrls,MMSystem;

type
  TMainForm = class(TForm)

    mpCDPlayer: TMediaPlayer;
    Image1: TImage;

    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;

    LblStatus: TLabel;
    LblTrackTime: TLabel;
    LblCurTrk: TLabel;
    LblTrackLen: TLabel;
    LblTotalLen: TLabel;
    LblTotTrk: TLabel;


    tmUpdateTimer: TTimer;

    SBPanel: TPanel;
    sbTrack1: TSpeedButton;
    sbTrack2: TSpeedButton;
    sbTrack3: TSpeedButton;
    sbTrack4: TSpeedButton;
    sbTrack5: TSpeedButton;
    sbTrack6: TSpeedButton;
    sbTrack7: TSpeedButton;
    sbTrack8: TSpeedButton;
    sbTrack9: TSpeedButton;
    sbTrack10: TSpeedButton;
    sbTrack11: TSpeedButton;
    sbTrack12: TSpeedButton;
    sbTrack13: TSpeedButton;
    sbTrack14: TSpeedButton;
    sbTrack15: TSpeedButton;
    sbTrack16: TSpeedButton;
    sbTrack17: TSpeedButton;
    sbTrack18: TSpeedButton;
    sbTrack19: TSpeedButton;
    sbTrack20: TSpeedButton;

    procedure tmUpdateTimerTimer(Sender: TObject);
    procedure mpCDPlayerPostClick(Sender: TObject;
                                                                 Button: TMPBtnType);
    procedure FormCreate(Sender: TObject);
    procedure sbTrackClick(Sender: TObject);
    procedure FormClose(Sender: TObject; 
                                             var Action: TCloseAction);
  private
    { Private declarations }

    OldTrack, CurrentTrack: Byte;
    m, s: Byte;

    TotalTracks: Byte;
    TotalLengthM: Byte;
    TotalLengthS: Byte;

    procedure GetCDTotals;
    procedure ShowTrackNumber;
    procedure ShowTrackTime;
    procedure ShowCurrentTime;
    procedure ShowPlayerStatus;
    procedure AdjustSpeedButtons;
    procedure HighlightTrackButton;
    function TrackNumToString(InNum: Byte): String;

  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

const
  { Mang cha cac chuoi bieu dien so - ban co the thay nhng chuoi tng 
    trng  nay bang ten bai hat }
  NumStrings: array[1..20] of String[10] =
      ('One', 'Two', 'Three', 'Four', 'Five', 'Six', 
        'Seven', 'Eight', 'Nine','Ten', 'Eleven', 
        'Twelve', 'Thirteen', 'Fourteen', 'Fifteen',
        'Sixteen','Seventeen', 'Eighteen', 
         'Nineteen', 'Twenty');

  MSFormatStr = '%dm %ds';

  PlayButtons: TButtonSet = [btPause, btStop, 
                                                      btNext, btPrev];
  StopButtons: TButtonSet = [btPlay, btNext, btPrev];

{Ham nay oi so track thanh chuoi hien th}
function TMainForm.TrackNumToString(
                                                              InNum: Byte): String;
begin
  if (InNum > High(NumStrings)) 
                  or (InNum < Low(NumStrings)) then
    Result := IntToStr(InNum)    
  else
    Result := NumStrings[InNum]; 
end;

{Kiem tra va at lai trang thai cho cac nut nhan the hien track}
procedure TMainForm.AdjustSpeedButtons;
var
  i: integer;
begin
  for i := 0 to SBPanel.ControlCount - 1 do
     if SBPanel.Controls[i] is TSpeedButton then    
      with TSpeedButton(SBPanel.Controls[i]) do 
                                          Enabled := Tag <= TotalTracks;
end;


procedure TMainForm.GetCDTotals;
var
  TimeValue: longint;
begin
  mpCDPlayer.TimeFormat := tfTMSF;
  TimeValue := mpCDPlayer.Length;  

  TotalTracks := mci_Tmsf_Track(mpCDPlayer.Tracks);      

  TotalLengthM := mci_msf_Minute(TimeValue);       
  TotalLengthS := mci_msf_Second(TimeValue);     

  LblTotTrk.Caption := TrackNumToString(TotalTracks);
  LblTotalLen.Caption := Format(MSFormatStr, 
                                                [TotalLengthM, TotalLengthS]);

  AdjustSpeedButtons;
end;

procedure TMainForm.ShowPlayerStatus;
begin
  if mpCDPlayer.EnabledButtons = PlayButtons then
    with LblStatus do
    begin
      case mpCDPlayer.Mode of
        mpNotReady: Caption := 'Not Ready';
        mpStopped:  Caption := 'Stopped';
        mpSeeking:  Caption := 'Seeking';
        mpPaused:   Caption := 'Paused';
        mpPlaying:  Caption := 'Playing';
      end;
    end
  else if mpCDPlayer.EnabledButtons = StopButtons then
                   LblStatus.Caption := 'Stopped';
end;

procedure TMainForm.ShowCurrentTime;

begin
  m := mci_Tmsf_Minute(mpCDPlayer.Position);
  s := mci_Tmsf_Second(mpCDPlayer.Position);
  LblTrackTime.Caption := Format(MSFormatStr, [m, s]);
end;

procedure TMainForm.ShowTrackTime;
var
  Min, Sec: Byte;
  Len: Longint;
begin
  if CurrentTrack <> OldTrack then
  begin
    Len :=mpCDPlayer.TrackLength[
                         mci_Tmsf_Track(mpCDPlayer.Position)];
    Min := mci_msf_Minute(Len);
    Sec := mci_msf_Second(Len);
    LblTrackLen.Caption := Format(MSFormatStr, [m, s]);
  end;
  OldTrack := CurrentTrack;
end;

procedure TMainForm.HighlightTrackButton;
var
  i: longint;
begin

  for i := 0 to ComponentCount - 1 do
    if Components[i] is TSpeedButton then
      if TSpeedButton(Components[i]).Tag = CurrentTrack then
        TSpeedButton(Components[i]).Font.Color := clRed
      else
        TSpeedButton(Components[i]).Font.Color := clNavy;
end;

procedure TMainForm.ShowTrackNumber;
var
  t: byte;
begin
  t := mci_Tmsf_Track(mpCDPlayer.Position);          
  CurrentTrack := t;  
  
  LblCurTrk.Caption := TrackNumToString(t); 
  HighlightTrackButton;      
end;


procedure TMainForm.tmUpdateTimerTimer(
                                                           Sender: TObject);
begin
  if mpCDPlayer.EnabledButtons = PlayButtons then
  begin
    mpCDPlayer.TimeFormat := tfMSF;
    mpCDPlayer.TimeFormat := tfTMSF;

    ShowTrackNumber; 
    ShowTrackTime;   
    ShowCurrentTime; 
  end;
end;

procedure TMainForm.mpCDPlayerPostClick(
                                                          Sender: TObject;
                                                          Button: TMPBtnType);
begin
  Case Button of

    btPlay:  
      begin
        mpCDPlayer.EnabledButtons := PlayButtons;
        LblStatus.Caption := 'Playing';
      end;

    btPause: 
      begin
        mpCDPlayer.EnabledButtons := StopButtons;
        LblStatus.Caption := 'Paused';
      end;

    btStop: 
      begin
        mpCDPlayer.Rewind;
        mpCDPlayer.EnabledButtons := StopButtons;
        LblCurTrk.Caption := 'One';
        LblTrackTime.Caption := '0m 0s';
        LblStatus.Caption := 'Stopped';
      end;

    btPrev, btNext: 
      begin
        mpCDPlayer.Play;
        mpCDPlayer.EnabledButtons := PlayButtons;
        LblStatus.Caption := 'Playing';
      end;
  end;
end;


procedure TMainForm.FormCreate(Sender: TObject);
begin
  try
    mpCDPlayer.Open;    
    if mpCDPlayer.Mode = mpPlaying then
      LblStatus.Caption := 'Playing';
      GetCDTotals;      
      ShowTrackNumber;  
      ShowTrackTime;  
      ShowCurrentTime;  
      ShowPlayerStatus; 
  except
    on EMCIDeviceError do
    begin
      ShowMessage('Error Initializing CD Player.  
                                               Program will now exit.');
       Application.Terminate;
    end;
  end;

  case mpCDPlayer.Mode of
    mpPlaying: 
                     mpCDPlayer.EnabledButtons := PlayButtons;
    mpStopped, mpPaused: 
                     mpCDPlayer.EnabledButtons := StopButtons;
  end;

end;

procedure TMainForm.sbTrackClick(Sender: TObject);
begin

  mpCDPlayer.Stop;  
  mpCDPlayer.StartPos := mpCDPlayer.TrackPosition[
                                                 (Sender as TSpeedButton).Tag];

  mpCDPlayer.Play;
  mpCDPlayer.EnabledButtons := PlayButtons;
  LblStatus.Caption := 'Playing';
end;

procedure TMainForm.FormClose(Sender: TObject;
                                                        var Action: TCloseAction);
begin
     mpCDPlayer.Close;  
end;

end.
